
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;

namespace Tathata.Web.Administration
{
    public partial class Administration_MessageTemplateDetails : BaseNopAdministrationPage
    {
        private void BindData()
        {
            MessageTemplate messageTemplate = MessageTemplateManager.GetByMessageTemplateID(this.MessageTemplateID);
            if (messageTemplate != null)
            {
                this.lblName.Text = Server.HtmlEncode(messageTemplate.Name);
                this.txtSubject.Text = messageTemplate.Subject;
                this.txtBody.Value = messageTemplate.Body;
            }
            else
                Response.Redirect("MessageTemplates.aspx");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    MessageTemplate messageTemplate = MessageTemplateManager.GetByMessageTemplateID(this.MessageTemplateID);

                    if (messageTemplate != null)
                    {
                        messageTemplate = MessageTemplateManager.UpdateMessageTemplate(messageTemplate.MessageTemplateID,
                        messageTemplate.Name, txtSubject.Text, txtBody.Value);
                        Response.Redirect("MessageTemplateDetails.aspx?MessageTemplateID=" + messageTemplate.MessageTemplateID.ToString());
                    }
                    else
                        Response.Redirect("MessageTemplates.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            try
            {
                MessageTemplateManager.DeleteMessageTemplate(this.MessageTemplateID);
                Response.Redirect("MessageTemplates.aspx");
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }

        public int MessageTemplateID
        {
            get
            {
                return CommonHelper.QueryStringInt("MessageTemplateID");
            }
        }
    }
}
